package com.haoshuang.zhouzhoubang.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Message;

import com.alipay.sdk.app.AuthTask;
import com.alipay.sdk.app.PayTask;
import com.haoshuang.zhouzhoubang.config.Config;
import com.haoshuang.zhouzhoubang.model.bean.WxPayBean;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

/**
 * Author: ly-yjm
 * Date: 2018/9/30
 * Explain: 支付帮助类  包含  支付宝支付
 */
public class PaymentUtil {
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;

    /**
     * 支付宝支付
     */
    public static void payAlipay(final Context mContext, String orderNo) {

        final String orderInfo = orderNo;   // 订单信息
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask((Activity) mContext);
                Map<String, String> result = alipay.payV2(orderInfo, true);

                Message msg = new Message();
                msg.what = SDK_PAY_FLAG;
                msg.obj = result;
             //   mHandler.sendMessage(msg);
            }
        };
        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }
    /**
     * 支付宝授权
     */
    public static void autoAlipay(final Context mContext, String orderNo) {
        final String orderInfo = orderNo;   // 订单信息
        Runnable authRunnable = new Runnable() {

            @Override
            public void run() {
                // 构造AuthTask 对象
                AuthTask authTask = new AuthTask((Activity) mContext);
                // 调用授权接口，获取授权结果
                Map<String, String> result = authTask.authV2(orderInfo, true);

                Message msg = new Message();
                msg.what = SDK_AUTH_FLAG;
                msg.obj = result;
            //    mHandler.sendMessage(msg);
            }
        };

        // 必须异步调用
        Thread authThread = new Thread(authRunnable);
        authThread.start();
    }
    public static void wxShareALl2(Context context, Bitmap url, boolean isTimeLineCb, String path) {

        IWXAPI mWXApi = WXAPIFactory.createWXAPI(context, Config.WX_ID, true);
        mWXApi.registerApp(Config.WX_ID);

        WXImageObject wxImageObject = new WXImageObject();
        wxImageObject.imagePath=path;

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = wxImageObject;


        SendMessageToWX.Req req = new SendMessageToWX.Req();
//WXSceneTimeline朋友圈    WXSceneSession聊天界面
        req.scene = isTimeLineCb ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;//聊天界面
        req.message = msg;
        req.transaction = String.valueOf(System.currentTimeMillis());
        mWXApi.sendReq(req);

    }
    /**
     * 微信支付
     */
    private IWXAPI api;

    public static void payWxPayment(IWXAPI api, WxPayBean wxBean) {
        PayReq req = new PayReq();
        req.appId			= Config.WX_ID;
        req.partnerId		= wxBean.getPartnerId();
        req.prepayId		= wxBean.getPrepayId();
        req.nonceStr		= wxBean.getNonceStr();
        req.timeStamp		= wxBean.getTimeStamp();
        req.packageValue	= wxBean.getPackage();
        req.sign			= wxBean.getSign();
        req.extData			= ""; // optional
        api.sendReq(req);
    }


}
